/**
 * 
 */
package dao;

import java.util.ArrayList;
import java.util.List;

import model.Conta;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author Gonze
 * 
 */
public class ContaDAO {

	protected String tableName = "Conta";

	private SQLiteDatabase database = null;

	public ContaDAO(Context ctx) {
		database = ctx.openOrCreateDatabase("gigo_financeiro",
				Context.MODE_PRIVATE, null);
	}

	public long insert(Conta object) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("descricao", object.getDescricao());
		contentValues.put("nome", object.getNome());
		contentValues.put("saldoinicial", object.getSaldoInicial());
		return database.insert(tableName, null, contentValues);
	}

	public void update(Conta object) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("descricao", object.getDescricao());
		contentValues.put("nome", object.getNome());
		contentValues.put("saldoinicial", object.getSaldoInicial());
		database.update(tableName, contentValues, "where idconta = ?",
				new String[] { String.valueOf(object.getIdConta()) });
	}

	public void delete(Conta object) {
		database.delete(tableName, "where idconta = ?",
				new String[] { String.valueOf(object.getIdConta()) });
	}

	public List<Conta> getList() {
		List<Conta> lista = new ArrayList<Conta>();
		Cursor cursor = null;
		try {
			cursor = database.query(tableName, new String[] { "idconta",
					"descricao", "nome", "saldoinicial" }, null, null, null,
					null, null);
			if (cursor.getCount() < 0) {
				return lista;
			}
			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToPosition(i);
				Conta conta = new Conta();
				conta.setIdConta(cursor.getInt(0));
				conta.setDescricao(cursor.getString(1));
				conta.setNome(cursor.getString(2));
				conta.setSaldoInicial(cursor.getDouble(3));
				lista.add(conta);
			}
			return lista;
		} catch (SQLException e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		} catch (Exception e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		}
	}

	public Conta getConta(int idConta) {
		Conta conta = new Conta();
		Cursor cursor = null;
		try {
			cursor = database.query(tableName, new String[] { "idconta",
					"descricao", "nome", "saldoinicial" }, "idconta = ?",
					new String[] { String.valueOf(idConta) }, null, null, null);
			if (cursor.getCount() < 0) {
				return null;
			}
			cursor.moveToFirst();
			conta.setIdConta(cursor.getInt(0));
			conta.setDescricao(cursor.getString(1));
			conta.setNome(cursor.getString(2));
			conta.setSaldoInicial(cursor.getDouble(3));
			return conta;
		} catch (SQLException e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		} catch (Exception e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		}
	}

}
